/*
 * @Author: liu
 * @Date: 2024-08-26 00:26:11
 * @LastEditTime: 2025-07-24 21:52:24
 * @LastEditors: 鱿鱼溪学院
 * @Description:
 * @FilePath: \yyx-web\src\utils\storage.ts
 * Copyright 鱿鱼溪学院 by edu v1.0 天道酬勤
 */
/**
 * @description:设置数据
 * @param {string} key
 * @param {any} value
 * @return {*}
 */
export const setItem = (key: string, value: any) => {
  // 将数组、对象类型的数据转化为 JSON 字符串进行存储
  if (typeof value === 'object') {
    value = JSON.stringify(value)
  }
  window.localStorage.setItem(key, value)
}

/**
 * 获取数据
 */
export const getItem = (key: string) => {
  const data = window.localStorage.getItem(key)
  try {
    return JSON.parse(data as string)
  } catch (err) {
    return data
  }
}

/**
 * 删除数据
 */
export const removeItem = (key: string) => {
  window.localStorage.removeItem(key)
}

/**
 * 删除所有数据
 */
export const removeAllItem = () => {
  window.localStorage.clear()
}
